<?php

declare(strict_types=1);

namespace app\api\controller;

use app\api\logic\User as LogicUser;
use app\common\logic\Payment as LogicPayment;

class User extends Auth
{
    /**
     * 个人中心
     */
    public function getMine()
    {
        $data = LogicUser::getMine($this->uid);
        return data($data, '个人中心');
    }

    /**
     * 更新用户信息 (wx.getUserProfile)
     */
    public function update(array $data)
    {
        LogicUser::update($this->uid, $data);
        return data($data, '更新成功');
    }

    /**
     * 消息解密获取手机号 (getPhoneNumber)
     *
     * @param string $iv
     * @param string $encryptedData
     */
    public function decryptPhoneNumber(string $iv, string $encryptedData)
    {
        $phone = LogicUser::decryptPhoneNumber($this->uid, $iv, $encryptedData);
        return data(['phone' => $phone], '获取手机号');
    }

    /**
     * 用户提现
     *
     * @param float $amount 提现金额
     */
    public function withdraw(float $amount)
    {
        // 格式化金额
        $amount = formatMoney($amount);
        $amount < 0.3 && fault('提现金额不能低于0.3元');
        // 执行提现逻辑
        LogicPayment::toBalance($this->uid, $amount);
        return success('提现成功');
    }
}
